-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add syntax to create a XorT from an F[A] and from a Xor. #1155
Add syntax to create a XorT from an F[A] and from a Xor. #1155
Conversation
d0d8fe0
to
7a7eb70
Compare
@@ -228,7 +229,7 @@ class XorTests extends CatsSuite { | |||
} | |||
} | |||
|
|||
test("combine is right iff both operands are right") { | |||
test("combine is right if both operands are right") { | |||
forAll { (x: Int Xor String, y: Int Xor String) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, I wonder why you changed this. This test seems testing iff
to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, thought it was a typo 😉
7a7eb70
to
d075224
Compare
Current coverage is 88.78%@@ master #1155 diff @@
==========================================
Files 232 233 +1
Lines 3073 3077 +4
Methods 3021 3024 +3
Messages 0 0
Branches 49 50 +1
==========================================
+ Hits 2729 2732 +3
- Misses 344 345 +1
Partials 0 0
|
d075224
to
462d3e8
Compare
* }}} | ||
*/ | ||
def toXorT[F[_], L, R](implicit F: Applicative[F], evL: A <:< L, evR: B <:< R): XorT[F, L, R] = | ||
XorT(F.pure(bimap(evL, evR))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm if toXorT
has to respecify all type parameters I am not sure I will favor it over fromXor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, that is what I was thinking. What do you think about adding it as a syntax method (see syntax.xor.toXorT2
) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I wasn't thinking about the variance on Xor
when I proposed this previously. The syntax approach doesn't look bad, but I want to ponder our options a bit.
I vote for the |
For anyone who has asked about what the downsides of variance are, this is an example :) What if we were to put something like this in the final implicit class XorOps[A, B](val value: A Xor B) extends AnyVal {
def toXorT[F[_]:Applicative]: XorT[F, A, B] = XorT.fromXor(value)
} If we do that then people should automatically pick up the syntax on their We could also link to What do you think? |
dc8f34a
to
374fbeb
Compare
new XorTOps[U.M, U.A](U.subst(fa))(U.TC) | ||
} | ||
|
||
final class XorTOps[F[_]: Functor, A](val fa: F[A]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call this XorTFunctorOps
or something? We could conceivably want to add XorT
-related syntax onto types other than F[A]
. I think it should still be mixed in via XorTSyntax
though.
374fbeb
to
3948f34
Compare
Many thanks! 👍 |
implicit def catsSyntaxXorT[F[_]: Functor, A](fa: F[A]): XorTFunctorOps[F, A] = new XorTFunctorOps(fa) | ||
} | ||
|
||
trait XorTSyntax1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to make this private (as per #1160).
3948f34
to
258fac6
Compare
Thanks for the comments @ceedubs and @kailuowang. Even a simple PR like this looks a lot nicer after improving the little issues you pointed out. |
👍 excellent. Thank you @peterneyens! |
I have added syntax to create a
XorT
from aF[A]
:fa.leftXorT
andfa.rightXorT
and which useXorT.left
andXorT.right
Xor
:xor.toXorT
(which was suggested by @ceedubs in Add convenience wrapper for Xor.fromOption in XorT #1078)The last one is a little bit difficult:
Xor
I need to work around the covariance ofA
andB
inXor
versus the invariance inXorT
, so you would need to writexor.toXorT[F, A, B]
.toXorT
(currentlytoXorT2
insyntax.xor
) as a syntax function onXor
so you can writexor.toXorT[F]
. But it's probably not expected that there are syntax functions for a data type defined in cats itself (at least I couldn't think of / find an earlier precedent).I prefer the syntax function because it is nicer/easier to use, but it would probably be more difficult to find, since it won't be in the
Xor
scaladoc. WDYT ?